package com.syc.springsecurityoauth2demo.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


/**
 * 资源
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 获取当前登录用户信息
     *
     * @param authentication
     * @return
     */
    @RequestMapping("getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        if (null != authentication && !"".equals(authorization)) {
            String token = authorization.substring(authorization.indexOf("Bearer ") + 7);
            return Jwts.parser()
                    .setSigningKey("wevirqoeri+qjrolw_eiq".getBytes(StandardCharsets.UTF_8))
                    .parseClaimsJws(token)
                    .getBody();
        }
        return authentication.getPrincipal();
    }
}
